﻿Imports System.Xml


Public Class Form1
    Dim int As Integer = 0
    Dim xmldoc As New XmlDocument
    Dim directory As String
    Dim HomePage As String = My.Settings.HomePage
    Friend WithEvents browser As Skybound.Gecko.GeckoWebBrowser


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim browser As New Skybound.Gecko.GeckoWebBrowser


        BrowserTabs.TabPages.Add("New Tab")
        browser.Name = "Web Browser"
        browser.Dock = DockStyle.Fill
        BrowserTabs.SelectedTab.Controls.Add(browser)

        AddHandler browser.DocumentCompleted, AddressOf Done
        int = int + 1

        CType(BrowserTabs.SelectedTab.Controls.Item(0), Skybound.Gecko.GeckoWebBrowser).Navigate("http://www.google.com")
        
        LoadBookmarks()
    End Sub

    Private Sub Loading(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs)

        ToolStripProgressBar1.Value = e.CurrentProgress
    End Sub
    Private Sub Done(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        BrowserTabs.SelectedTab.Text = CType(BrowserTabs.SelectedTab.Controls.Item(0), Skybound.Gecko.GeckoWebBrowser).DocumentTitle
        UrlBox.Text = CType(BrowserTabs.SelectedTab.Controls.Item(0), Skybound.Gecko.GeckoWebBrowser).Url.ToString

    End Sub

    Private Sub GoButtonNavigate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
        CType(BrowserTabs.SelectedTab.Controls.Item(0), Skybound.Gecko.GeckoWebBrowser).Navigate(UrlBox.Text)
        If UrlBox.Text IsNot Nothing Then
            If Not UrlBox.Items.Contains(UrlBox.Text) Then
                UrlBox.Items.Add(UrlBox.Text)
            End If
        End If
    End Sub


    Private Sub BackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CType(BrowserTabs.SelectedTab.Controls.Item(0), Skybound.Gecko.GeckoWebBrowser).GoBack()
    End Sub

    Private Sub ButtonNavigate(ByVal sWebsite As String)
        Dim xPath As String = "//WebSite[Name='" & sWebsite & "']/URL"
        Dim xn As XmlNode = xmldoc.SelectSingleNode(xPath)

        CType(BrowserTabs.SelectedTab.Controls.Item(0), Skybound.Gecko.GeckoWebBrowser).Navigate(xn.InnerText)
    End Sub


  


    Private Sub OptionsMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsMenu.Click
        Dialog1.ShowDialog()
    End Sub


    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        SaveFileDialog1.ShowDialog()
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        PrintDialog1.ShowDialog()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Public Sub LoadBookmarks()
        Try
            xmldoc.Load(directory)
        Catch ex As Exception



            FolderBrowserDialog1.ShowDialog()
            directory = FolderBrowserDialog1.SelectedPath.ToString
            My.Settings.Directory = FolderBrowserDialog1.SelectedPath.ToString
            My.Settings.Save()
        End Try
        xmldoc.Load(My.Settings.Directory & "\WebSites.xml")
        BookmarksBar.Items.Clear()

        ' Loop through the XML file and add a toolstrip button for each
        ' user defined Web site.
        Dim websiteNodes As XmlNodeList
        Dim webSiteNode As XmlNode
        websiteNodes = xmldoc.GetElementsByTagName("WebSite")
        Dim iCount As Integer = 1

        For Each webSiteNode In websiteNodes
            Dim eachSiteNodes As XmlNodeList
            Dim eachSiteNode As XmlNode
            eachSiteNodes = webSiteNode.ChildNodes

            For Each eachSiteNode In eachSiteNodes
                If eachSiteNode.Name = "Name" Then
                    AddBookmarkButton("tsWebsite" & iCount.ToString, eachSiteNode.InnerText)
                    iCount += 1
                End If
            Next
        Next
    End Sub

    Private Sub ManageBookmarksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageBookmarksToolStripMenuItem.Click
        Dim manage As New Dialog2
        manage.ShowDialog()
        LoadBookmarks()
    End Sub

    Public Sub AddBookmarkButton(ByVal buttonName As String, ByVal webSiteName As String)
        Dim NewButton As New System.Windows.Forms.ToolStripButton
        NewButton.Name = buttonName
        NewButton.Text = webSiteName
        NewButton.DisplayStyle = ToolStripItemDisplayStyle.Text
        AddHandler NewButton.Click, AddressOf NewButton_Click
        BookmarksBar.Items.Add(NewButton)
    End Sub

    Private Sub NewToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub NewButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim aButton As ToolStripButton = CType(sender, ToolStripButton)
        ButtonNavigate(aButton.Text)
    End Sub

    
    Private Sub RefreshBookmarksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshBookmarksToolStripMenuItem.Click
        LoadBookmarks()
    End Sub

    Private Sub AddNewMarkToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewMarkToolStripMenuItem.Click

    End Sub
End Class

